package com.itbour.web.action.tg;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.itbour.com.page.Page;
import com.itbour.common.base.BaseAction;
import com.itbour.dto.com.PartnerIncomeInfoDto;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.tg.PartnerOrderInfDto;
import com.itbour.dto.tg.PartnerSaleDto;
import com.itbour.service.tg.PartnerOrderInfService;
import com.itbour.web.action.material.ItbComWebAction;
import com.opensymphony.xwork2.ActionContext;

/**
 * @author LQ 订单明细
 * */
public class GetPartnerOrderDetailAction extends BaseAction {

	/**
	 */
	private static final long serialVersionUID = 1L;
	private PartnerOrderInfService partnerOrderInfService;
	private List<PartnerOrderInfDto> listResult;
	private Map<String, Object> mapResult=new HashMap<String, Object>();
	
	private PartnerIncomeInfoDto partnerIncomeInfo;

	/**
	 * @return listResult
	 */
	public String getPartnerOrderInf() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto) session.get("user");
		HttpServletRequest request = ServletActionContext.getRequest();
		String uId = request.getParameter("uId");
		String startDt = request.getParameter("startDt");
		String endDt = request.getParameter("endDt");
		String pageNumber = request.getParameter("page");
		String pageSize = request.getParameter("rows");
		if (user != null && user.getUser_id() != null && uId != null
				&& startDt != null && endDt != null) {
			Map<String, Object> parm = new HashMap<String, Object>();
			parm.put("user_id", uId);
			parm.put("start_time", startDt);
			parm.put("end_time", endDt);
			Page pa = partnerOrderInfService.getPartnerOrderInfo(parm,Integer.parseInt(pageNumber),Integer.parseInt(pageSize));
			Map<String, Object> result=new HashMap<String, Object>();
			result.put("total", pa.getTotalCount());
			result.put("rows", pa.getResult());
			mapResult=result;
		}
		return SUCCESS;
	}

	/**查看合伙人销售排行榜
	 * @return
	 * @throws Exception 
	 */
	public String getPartnerSaleRank() throws Exception{
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		HttpServletRequest request =ServletActionContext.getRequest();
		if(user!=null && user.getUser_id()!=null){
			Integer pageNo=Integer.parseInt(request.getParameter("page"));
			Integer pageSize =Integer.parseInt(request.getParameter("rows"));
			Integer year = Integer.parseInt(request.getParameter("year"));
			Integer month = Integer.parseInt(request.getParameter("month"));
			Integer uId = Integer.parseInt(request.getParameter("uId"));
			if(uId!=null&&year!=null&&month!=null){
				Map<String, Object> parm=new HashMap<String, Object>();
				parm.put("uId",uId);
				parm.put("year",year);
				parm.put("month",month);
				Page p=partnerOrderInfService.getPartnerSaleRank(parm, pageNo, pageSize);
				mapResult.put("total", p.getTotalCount());
				mapResult.put("rows", p.getResult());
			}else{
				mapResult.put("error", "parmer is null");
			}
		}else{
			mapResult.put("error","no");
		}
		return SUCCESS;
	}
	
	// 计算月奖金
	public String calPartnerBonus() throws Exception {
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		HttpServletRequest request =ServletActionContext.getRequest();
		if(user!=null  && user.getUser_id()>0
				&& request!=null
				&& partnerIncomeInfo!=null && partnerIncomeInfo.getPartner_id()>0) {
			Long bonus = 0L;// 奖金
			
			Map<String, Object> parm=new HashMap<String, Object>();
			parm.put("uId",partnerIncomeInfo.getUser_id());
			parm.put("year",partnerIncomeInfo.getYear());
			parm.put("month",partnerIncomeInfo.getMonth());
			Page p=partnerOrderInfService.getPartnerSaleRank(parm, 1, 50);// 尝试取前50个排名
			
			// 合伙人数>=限制条件的指定人数时 才计算奖金
			if(p.getTotalCount()>0 && p.getTotalCount() >= partnerIncomeInfo.getBonusLim()) {
				// 第一名是目标合伙人
				List<PartnerSaleDto> list = (List<PartnerSaleDto>)p.getResult();
				if(list!=null && list.get(0).getPartnerId()==partnerIncomeInfo.getPartner_id()) {
					// 计算奖金
					Map<String, Object> param2 = new HashMap<String, Object>();
					param2.put("partner_id", partnerIncomeInfo.getPartner_id());
					
					String startDt = ItbComWebAction.getFirstDayOfMonth(partnerIncomeInfo.getYear(),
							partnerIncomeInfo.getMonth()) + " 00:00:00";
					param2.put("startDt", startDt);
					
					String endDt = ItbComWebAction.getLastDayOfMonth(partnerIncomeInfo.getYear(),
							partnerIncomeInfo.getMonth()) + " 23:59:59";
					param2.put("endDt", endDt);
					
					bonus = partnerOrderInfService.calPartnerBonus(param2);
				}
			}
			partnerIncomeInfo.setIncome_bonus(bonus);
			return SUCCESS;
		}
		
		return ERROR;
	}
	
	
	public PartnerOrderInfService getPartnerOrderInfService() {
		return partnerOrderInfService;
	}

	public void setPartnerOrderInfService(
			PartnerOrderInfService partnerOrderInfService) {
		this.partnerOrderInfService = partnerOrderInfService;
	}

	public List<PartnerOrderInfDto> getListResult() {
		return listResult;
	}

	public void setListResult(List<PartnerOrderInfDto> listResult) {
		this.listResult = listResult;
	}

	public Map<String, Object> getMapResult() {
		return mapResult;
	}

	public void setMapResult(Map<String, Object> mapResult) {
		this.mapResult = mapResult;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public PartnerIncomeInfoDto getPartnerIncomeInfo() {
		return partnerIncomeInfo;
	}

	public void setPartnerIncomeInfo(PartnerIncomeInfoDto partnerIncomeInfo) {
		this.partnerIncomeInfo = partnerIncomeInfo;
	}
	
}
